package com.corewillsoft.fireflies.ui;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.HorizontalAlign;

import android.content.Context;
import android.graphics.Color;

import com.corewillsoft.fireflies.storage.objects.MapLevel;

public class MapLevelTexture extends Rectangle {

	private static final float LEVEL_ICON_MARGIN = 5;
	private Engine engine;
	private Context context;
	private MapLevel mapLevel;
	private boolean isLevelAvailable;
	private Font levelSizeFont;
	private Sprite levelIcon;
	private Text levelText;

	public MapLevelTexture(MapLevel mapLevel, Engine engine, Context context) {
		super((float) mapLevel.getCoordinate().getX(),
				(float) mapLevel.getCoordinate().getY(),
				mapLevel.getWidth(), mapLevel.getHeight());
		this.engine = engine;
		this.context = context;
		this.mapLevel = mapLevel;
		initFont();
		addLevelTextures();
	}

	private void initFont() {
		BitmapTextureAtlas arialFontTexture = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		levelSizeFont = FontFactory.createFromAsset(arialFontTexture,
				context, "Arial.ttf", 24, true, Color.WHITE);
		engine.getTextureManager().loadTexture(arialFontTexture);
		engine.getFontManager().loadFont(levelSizeFont);
	}

	private void addLevelTextures() {
		addLevelIcon();
		addLevelText();
	}

	private void addLevelIcon() {
		BitmapTextureAtlas bitmapTextureAtlas = new BitmapTextureAtlas(128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		String imagePath = "stop.png";
		if (isLevelAvailable) {
			imagePath = mapLevel.getImageRef();
		}

		TextureRegion textureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas,
				context, imagePath, 0, 0);
		engine.getTextureManager().loadTexture(bitmapTextureAtlas);

		levelIcon = new Sprite(LEVEL_ICON_MARGIN, LEVEL_ICON_MARGIN, mapLevel.getWidth() - 2 * LEVEL_ICON_MARGIN,
				mapLevel.getHeight() - 2 * LEVEL_ICON_MARGIN, textureRegion);
		attachChild(levelIcon);
	}
	
	private void addLevelText() {
		levelText = new Text(0, 0, this.levelSizeFont, "Level " + mapLevel.getLevel(), HorizontalAlign.LEFT);
		float baseHeight = levelText.getBaseHeight();
		float baseWidth = levelText.getBaseWidth();
		levelText.setColor(0f, 0f, 0f);
		levelText.setPosition((mapLevel.getWidth() - baseWidth) / 2, (mapLevel.getHeight() - baseHeight) / 2);
		attachChild(levelText);
	}

	public void setMapLevelAvailable(boolean available) {
		if (isLevelAvailable != available) {
			this.isLevelAvailable = available;
			detachChild(levelIcon);
			detachChild(levelText);
			addLevelIcon();
			addLevelText();
		}
	}

	public MapLevel getMapLevel() {
		return mapLevel;
	}
	
	public void setSelected(boolean selected) {
	}
}
